package cn.codergege.demo.util

import groovy.sql.Sql

import org.apache.commons.dbcp2.BasicDataSource
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class DbUtil {
    private static final Logger log = LoggerFactory.getLogger(DbUtil.class)
    static Sql getSql() {
        def ds = new BasicDataSource()
        ds.with {
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'base'
            password = 'base'
            url = 'jdbc:oracle:thin:@10.22.10.121:1521:ywgs'
        }
        return new Sql(ds)
    }
    static prepare() {
        log.info('preparing database ...')

        def ddls = [
                '''drop table if exists t_athlete;
''',
                '''create table if not exists t_athlete (
id int primary key auto_increment,
firstname varchar(100),
lastname varchar(100),
birthday date
) engine innodb default charset utf8;

''', '''
insert into t_athlete (firstname, lastname, birthday) 
values('zhao', 'bo', '1986-05-29');
''', '''
insert into t_athlete (firstname, lastname, birthday) 
values('d', 'jh', '1988-03-22');
''', '''
insert into t_athlete (firstname, lastname, birthday) 
values('b', 'b', '2017-11-11');
'''
        ];
        def sql = getSql()
        ddls.each {
            sql.execute(it)
        }
        sql.close()
        log.info('done.')
    }
}
